﻿using AdminSystem.Application.Commands.TraceabilityCommands;
using AdminSystem.Domain.Entities;
using AdminSystem.EntityFramworkCore;
using AdminSystem.Repositories.Interfaces;
using AutoMapper;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AdminSystem.Application.CommandHandlers.TraceabilityCommandHandlers
{
    public class TraceabilityUpdCommandHandler : IRequestHandler<TraceabilityUpdCommand, APIResult<object>>
    {
        private readonly IBaseRepositories<Traceability> traceabilityBase;
        private readonly IMapper mapper;
        public TraceabilityUpdCommandHandler(IBaseRepositories<Traceability> traceabilityBase, IMapper mapper)
        {
            this.traceabilityBase = traceabilityBase;
            this.mapper = mapper;
        }
        public Task<APIResult<object>> Handle(TraceabilityUpdCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code = ResultCode.更新成功;
            result.Message = ResultCode.更新成功.ToString();
            Traceability entity = mapper.Map<Traceability>(request);
            entity.Id = request.Id;
            entity.TraceabilityId = request.TraceabilityId;
            entity.ShopId = request.ShopId;
            entity.ProBatch = request.ProBatch;
            entity.ShelfLife = request.ShelfLife;
            entity.ProLicense = request.ProLicense;
            entity.Manufacturers = request.Manufacturers;
            entity.CoverPic = request.CoverPic;
            entity.IsDelete = request.IsDelete;
            traceabilityBase.Update(entity);
    
            return Task.FromResult(result);
        }
    }
}
